﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Configuration;

namespace ProjectQuaDep
{
    public partial class JCropList : System.Web.UI.Page
    {
        protected string urlImages = "";
        protected string id = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] == null ? "0" : Request.QueryString["id"].ToString();
            DataTable dt = new DataTable();
            dt = BO.Admin.ImagesBO.GetInfomationImagesBO(id);
            if (dt.Rows.Count > 0)
            {
                urlImages = dt.Rows[0][BO.Const.tbImages.Img].ToString();
                urlImages = resizeanh(urlImages, "/Upload/Web/thumbimages/", 800);
                //UpdateImageThumb(string ID, string url)
            }
        }
        private string resizeanh(string filename, string thumuc, int thumbWidth)
        {
            string linkFileRe = "N/A";
            string pathAvatars = Server.MapPath(thumuc);
            //try
            //{
            int thumbHeight;
            System.Drawing.Image oImg = System.Drawing.Image.FromFile(Server.MapPath(filename));//Bitmap.FromFile(Server.MapPath(filename));
            thumbHeight = (int)(oImg.Height * ((float)thumbWidth / (float)oImg.Width));
            System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);


            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighSpeed;
            oGraphic.SmoothingMode = SmoothingMode.HighSpeed;
            oGraphic.InterpolationMode = InterpolationMode.High;

            Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
            oGraphic.DrawImage(oImg, oRectangle);
            string nameAdd = "_ThumbList_";
            string filesource = Server.MapPath(thumuc) + nameAdd + Path.GetFileName(filename);


            if (File.Exists(filesource))
            {
                File.Delete(filesource);
            }
            else
            {
                oThumbNail.Save(filesource, ImageFormat.Jpeg);
                oImg.Dispose();
            }
            linkFileRe = thumuc + nameAdd + Path.GetFileName(filename);
            //add chữ vào
            //}
            //catch (Exception ex)
            //{
            //    Response.Write("Error: " + ex.Message);
            //}
            return linkFileRe;
        }
        public static string GenerateCropUserImagesFolder()
        {
            return "upload\\web\\userimages\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";
        }
    }
}